﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using OurFAS.Data;
using GetHashValue;
using System.Transactions;

namespace OurFAS.MySystem
{
    public partial class FrmRoleDetail : Form
    {
        #region 成员变量
        private string _sysRoleCode = "01";
        private Base_Role _role;
        private EnumHelper.EnumObjctOperateType _opType;
        #endregion

        #region  构造函数
        public FrmRoleDetail(EnumHelper.EnumObjctOperateType opType, Base_Role role)
        {
            InitializeComponent();
            _role = role;
            _opType = opType;
        }
        #endregion

        #region  控件函数
        private void FrmRoleDetail_Load(object sender, EventArgs e)
        {
            //初始化Combobox等控件的绑定内容
            OurFasDBEntities entity = new OurFasDBEntities();
            List<Base_Menu> menuList = entity.Base_Menu.Where(c => c.RoleCode == _sysRoleCode).ToList();
            clbAuthorityList.DataSource = menuList;
            clbAuthorityList.ValueMember = "MenuTitle";

            switch (_opType)
            {
                case EnumHelper.EnumObjctOperateType.New:
                    {
                        //控件可用性设置
                    }
                    break;
                case EnumHelper.EnumObjctOperateType.Edit:
                    {

                        IList<Base_Menu> roleMenuList 
                            = entity.Base_Menu.Where(c => c.RoleCode.Trim() == _role.RoleCode).ToList();
                        for (int i = 0; i < clbAuthorityList.Items.Count; i++)
                        {
                            Base_Menu menu = clbAuthorityList.Items[i] as Base_Menu;
                            foreach (Base_Menu roleMenu in roleMenuList)
                            {
                                if (menu.MenuCode == roleMenu.MenuCode)
                                {
                                    clbAuthorityList.SetItemChecked(i, true);
                                }
                            }
                        }

                        if (_role.RoleCode.Trim() == _sysRoleCode)
                        {
                            MessageBox.Show("注意，系统管理员不能进行编辑！");
                            btnConfirm.Enabled = false;
                        }
                    }
                    break;
            }
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            //数据完整性判断
            if (string.IsNullOrWhiteSpace(tbRoleCode.Text))
            {
                MessageBox.Show("角色代码不能为空!");
                return;
            }

            if (string.IsNullOrWhiteSpace(tbRoleName.Text))
            {
                MessageBox.Show("角色名称不能为空!");
                return;
            }


            if (MessageBox.Show("您确定要添加（修改）该角色吗？", "注意", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
            {
                if (_opType == EnumHelper.EnumObjctOperateType.New)
                {
                    Base_Role rec = new Base_Role();
                    rec.RoleCode = tbRoleCode.Text.Trim();
                    rec.RoleName = tbRoleName.Text.Trim();
                    //添加角色信息

                    OurFasDBEntities entityRole = new OurFasDBEntities();
                    entityRole.Base_Role.AddObject(rec);
                    
                        OurFasDBEntities entityMenu = new OurFasDBEntities();
                        //新建角色的话，不需要删除数据库中原有的角色权限

                        foreach (Object obj in clbAuthorityList.CheckedItems)
                        {
                            Base_Menu menu = obj as Base_Menu;
                            menu.RoleCode = rec.RoleCode;
                            entityMenu.Base_Menu.AddObject(menu);
                        }
                    
                   
                    try
                    {
                        using (TransactionScope scope = new TransactionScope())
                        {
                            entityRole.SaveChanges(System.Data.Objects.SaveOptions.AcceptAllChangesAfterSave);
                            entityMenu.SaveChanges(System.Data.Objects.SaveOptions.AcceptAllChangesAfterSave);

                            scope.Complete();
                        }
                        MessageBox.Show("新建角色成功！");
                        this.DialogResult = System.Windows.Forms.DialogResult.OK;
                        this.Close();
                    }
                    catch
                    {
                        MessageBox.Show("新建角色失败！");
                    }

                }
                else if (_opType == EnumHelper.EnumObjctOperateType.Edit)
                {
                    OurFasDBEntities entityRole = new OurFasDBEntities();
                    Base_Role rec
                        = entityRole.Base_Role.Where(c => c.RoleCode.Trim() == _role.RoleCode).FirstOrDefault();
                    if (rec != null)
                    {
                        rec.RoleCode = tbRoleCode.Text.Trim();
                        rec.RoleName = tbRoleName.Text.Trim();

                        OurFasDBEntities entityMenu = new OurFasDBEntities();
                        //新建角色的话，不需要删除数据库中原有的角色权限
                        List<Base_Menu> delMenuList 
                            = entityMenu.Base_Menu.Where(c => c.RoleCode.Trim() == _role.RoleCode).ToList();
                        foreach (Base_Menu menu in delMenuList)
                        {
                            entityMenu.DeleteObject(menu);
                        }
                        
                        foreach (Object obj in clbAuthorityList.CheckedItems)
                        {
                            Base_Menu menu = obj as Base_Menu;
                            menu.RoleCode = rec.RoleCode;
                            entityMenu.Base_Menu.AddObject(menu);
                        }
                        try
                        {
                            using (TransactionScope scope = new TransactionScope())
                            {
                                entityRole.SaveChanges(System.Data.Objects.SaveOptions.AcceptAllChangesAfterSave);
                                entityMenu.SaveChanges(System.Data.Objects.SaveOptions.AcceptAllChangesAfterSave);

                                scope.Complete();
                            }
                            MessageBox.Show("编辑角色成功！");
                            this.DialogResult = System.Windows.Forms.DialogResult.OK;
                            this.Close();
                        }
                        catch
                        {
                            MessageBox.Show("编辑角色失败！");
                        }
                    }
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }
        #endregion
    }
}
